<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>更新车辆信息</title>
		<script type="text/javascript" src="/admin/qrcode/jq.js"></script>
		<script type="text/javascript" src="/admin/qrcode/bot.js"></script>
		<link rel="stylesheet" type="text/css" href="/admin/css/boot.css"></link>
		<link rel="stylesheet" type="text/css" href="/admin/css/sl.css"></link>
		<style>
			label span{color: #ff0000; padding-right: 5px;}
		</style>
	</head>

	<body>
	<h2>修改车辆信息</h2>
	<form id="myForm" class="form-horizontal">
		<div class="form-group">
			<label class="col-sm-1 control-label"><span>*</span>车辆名称</label>
			<div class="col-sm-9">
				<input required="required" name="autoName" class="form-control" />
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-1 control-label"><span>*</span>价格</label>
			<div class="col-sm-9">
				<input required="" name="price" class="form-control"/>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-1 control-label"><span>*</span>级别</label>
			<div class="col-sm-9">
				<input name="ran" class="form-control"/>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-1 control-label"><span>*</span>能源</label>
			<div class="col-sm-9">
				<input name="energy" class="form-control"/>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-1 control-label"><span>*</span>车型</label>
			<div class="col-sm-9">
				<input name="model" class="form-control"/>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-1 control-label"><span>*</span>生产商</label>
			<div class="col-sm-9">
				<input name="firm" class="form-control"/>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-1 control-label"><span>*</span>发动机</label>
			<div class="col-sm-9">
				<input name="engine" class="form-control"/>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-1 control-label">最大功率</label>
			<div class="col-sm-9">
				<input name="power" class="form-control"/>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-1 control-label">变速箱</label>
			<div class="col-sm-9">
				<input name="gearbox" class="form-control"/>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-1 control-label">车身规格</label>
			<div class="col-sm-9">
				<input name="measure" class="form-control"/>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-1 control-label">车身结构</label>
			<div class="col-sm-9">
				<input name="bodyStuc" class="form-control"/>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-1 control-label">最高车速</label>
			<div class="col-sm-9">
				<input name="km" class="form-control"/>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-1 control-label">保修规则</label>
			<div class="col-sm-9">
				<input name="term" class="form-control"/>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-1 control-label"><span>*</span>是否为二手车</label>
			<div class="col-sm-9">
				<input name="used" class="form-control"/>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-1 control-label">使用年限</label>
			<div class="col-sm-9">
				<input name="utilityTime" class="form-control"/>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-1 control-label">行驶公里</label>
			<div class="col-sm-9">
				<input name="mileage" class="form-control"/>
			</div>
		</div>
			<div class="form-group">
				<label class="col-sm-1 control-label"></label>
				<div class="col-sm-9">
					<button id="btnUpdate" type="button" class="btn btn-sm btn-info">提&nbsp;交</button>
					<button type="reset"  class="btn btn-sm btn-warning">重&nbsp;置</button>
				</div>
			</div>
		</form>		
	</body>
</html>
<script>
	String.prototype.GetValue= function(para) {
		let reg = new RegExp("(^|&)"+ para +"=([^&]*)(&|$)");
		let r = this.substr(this.indexOf("\?")+1).match(reg);
		if (r!=null) return unescape(r[2]); return null;
	}
	$(function () {
		//根据地址获取teamId,如果null则执行增加，否则执行更新
		let url = document.location.toString();//获取URL
		let teamId=url.GetValue("teamId");
		let pageNum=url.GetValue("pageNum");
		let pageSize=url.GetValue("pageSize");
		//alert(teamId);
		queryById(teamId);
		//提交按钮的单击事件--实现真正的更新
		$("#btnUpdate").click(function () {
			$.ajax({
				type: "POST",//这里必须写POST
				url: "/Admin/autoUpdate/"+teamId,
				data: $("#myForm").serialize()+"&_method=PUT",
				dataType:"json",
				success: function(vo){
					if(vo.code===200) {
						window.location.href = "list.html";
					}else{
						alert("更新失败！"+vo.msg);
					}
				}
			});
		});
	});
	function queryById(teamId) {
		$.ajax({
			type: "GET",
			url: "/Admin/selectKey", //RESTful风格的API定义
			data: {id:teamId},
			success: function (vo) {
				let obj=vo;
				let teamLogo=obj.autoName;
				let teamName=obj.price;
				let chineseName=obj.ran;
				let coach=obj.energy;
				let stadium=obj.model;
				let location=obj.firm;
				let time=obj.timeMarket;
				let engine=obj.engine;
				let power=obj.power;
				let gearbox=obj.gearbox;
				let measure=obj.measure;
				let bodyStuc=obj.bodyStuc;
				let km = obj.km;
				let term = obj.term;
				let used = obj.used;
				let utilityTime= obj.utilityTime;
				let mileage = obj.mileage;
				//let area=obj.picture;
				$("input[name='autoName']").val(teamLogo);
				$("input[name='price']").val(teamName);
				$("input[name='ran']").val(chineseName);
				$("input[name='energy']").val(coach);
				$("input[name='model']").val(stadium);
				$("input[name='firm']").val(location);
				$("input[name='timeMarket']").val(time);
				$("input[name='engine']").val(engine);
				$("input[name='power']").val(power);
				$("input[name='gearbox']").val(gearbox);
				$("input[name='measure']").val(measure);
				$("input[name='bodyStuc']").val(bodyStuc);
				$("input[name='km']").val(km);
				$("input[name='term']").val(term);
				$("input[name='used']").val(used);
				$("input[name='utilityTime']").val(utilityTime);
				$("input[name='mileage']").val(mileage);
				//$("select[name='picture']").val(area);
				/*if(area!=null){
					$("img[name='teamLogo']").attr("src","/img/uploadFile/"+teamLogo);
				}else{
					$("img[name='teamLogo']").addClass("hidden");
					$("img[name='teamLogo']").parent().html("还没有上传logo图片");
				}*/
			}
		});
	}

</script>